/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.example.grid;

import java.io.IOException;

import chunmap.data.provider.LayerFactory;
import chunmap.raster.grid.GridLayer;
import chunmap.raster.grid.operate.ImageCut;
import chunmap.util.MyAssert;
import chunmap.view.Map;
import chunmap.view.View;
import chunmap.view.ViewPort;
import chunmap.view.layer.VectorLayer;
import chunmap.view.render.GeneralSymbol;
import chunmap.view.render.Painter;
import chunmap.view.render.Style2D;
import chunmap.view.render.Symbol;

/**
 * cut map to tileLayer
 * 
 * @author chunquedong
 * 
 */
public class ImageCutTest {

	private Map map;
	private static int buffer = 3;// 为了消除图片之间的缝隙,读取时和GridDataFactory.buffer保持一致。

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ImageCutTest test = new ImageCutTest();
		// test.cut();
		test.autoCut();
	}

	public void cut() {
		init(100, 80);

		String path = "D:\\Temp\\chunmapData";
		try {
			ImageCut imageCut = new ImageCut(map, path);
			imageCut.cut(2, "hello");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public GridLayer autoCut() {
		init(500, 400);

		String path = "D:\\Temp\\chunmapData";
		try {
			ImageCut imageCut = new ImageCut(map, path);
			return imageCut.autoCut(3, 3);

		} catch (IOException e) {
			e.printStackTrace();
		}

		throw new MyAssert.AssertionEexception("error on cut to tile");
	}

	private void init(int width, int height) {
		View view = new ViewPort(width, height,buffer);
		Painter painter = new Painter(width + buffer, height + buffer);

		map = new Map(view, painter);

		// Load data
		String path1=
				"D:\\ChunMap\\ChunMap1.1.1\\Data\\gs\\region.shp";
		String path2=
				"D:\\ChunMap\\ChunMap1.1.1\\Data\\gs\\city1.shp";
		String path3=
				"D:\\ChunMap\\ChunMap1.1.1\\Data\\gs\\river.shp";

		VectorLayer layer = (VectorLayer)  LayerFactory.openShapeFile(path1);
		layer.setDefaultStyle(new Symbol[]{new GeneralSymbol()});
		VectorLayer layer2 = (VectorLayer) LayerFactory.openShapeFile(path2);
		layer2.setDefaultStyle(new Symbol[]{new GeneralSymbol(Style2D.style2())});
		VectorLayer layer3 = (VectorLayer) LayerFactory.openShapeFile(path3);
		layer3.setDefaultStyle(new Symbol[]{new GeneralSymbol(Style2D.style1())});

		map.getLayerCollection().add(layer);
		map.getLayerCollection().add(layer2);
		map.getLayerCollection().add(layer3);

	}
}
